package Team

import (
	"sanguo/cluster"
	codecs "sanguo/codec/cs"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	ss_msg "sanguo/protocol/ss/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionReplyFightInvite struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	errcode cs_message.EnumType
}

func (this *transactionReplyFightInvite) GetModuleName() string {
	return "Team"
}

func (this *transactionReplyFightInvite) GetReplyMsg() proto.Message {
	return nil
}

func (this *transactionReplyFightInvite) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionReplyFightInvite) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionReplyFightInvite) Begin() {

	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()
	if nil == role || !role.IsRoleModleData() {
		node_game.Debugln(role.GetUserID(), "ReplyFightInvite fail no selected role")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	_team := role.Team
	if _team == nil {
		node_game.Infoln(role.GetUserID(), role.GetID(), "ReplyFightInvite fail: _team is nil ")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	msg := this.req.GetData().(*cs_message.ReplyFightInviteToS)
	req := &ss_msg.FightInvite{
		TeamID: proto.Uint64(_team.TeamID),
		RoleID: proto.Uint64(role.GetID()),
	}

	if msg.GetReply() == cs_message.FightInviteType_Agree {
		req.Reply = proto.Int32(1)
	} else if msg.GetReply() == cs_message.FightInviteType_DisAgree {
		req.Reply = proto.Int32(2)
	}

	cluster.PostMessage(node_game.GetTeamServer().ToPeerID(), req)
	this.user.EndTrans(this)

}

func (this *transactionReplyFightInvite) End() {
}

func (this *transactionReplyFightInvite) Timeout() {
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.ReplyFightInviteToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionReplyFightInvite{
			user: user,
			req:  msg,
		}
	})
}
